local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local signIn_cfg=ServerOHOH.config.DailyReward
local ItemTool=_G.CommonOHOH.load.ItemTool();

local DAY_TIME=24*60*60
local MAX_DAY=ServerOHOH.config.Game.SignIn_Count
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.dataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
	self.data = self.dataBase:GetData("sing_in");
end;

function M.GetDayStartAndEnd(self,time)
	local day=math.floor(time/DAY_TIME)*DAY_TIME
	return day,day+DAY_TIME
end

function M.AddSomething(self,data)
	self.ItemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl())
	for k,v in pairs(data.item_id) do
		local key,id=ItemTool.GetKeyAndID(v)
		local key,count= ItemTool.GetRewardByLevel(k,v,self.dataBase:GetData("base").level)
		self.ItemCtrl:AddAnyThing(key,count)
	end
end

function M.GetGift(self,id)
	self.data.id = id
	self.data.time = os.time()+DAY_TIME;
	self:AddSomething(signIn_cfg[id])
end


--获取签到ID，非连续
function M.GetSingInId(self)

	local day_s,end_s=self:GetDayStartAndEnd(self.data.time)

	if os.time()>end_s then

		local id=self.data.id+1
		if id>MAX_DAY then
			id=1
		end
		return id
	else
		return self.data.id,true --不能够领取
	end

end

--获取连续签到ID，如果超过一天没签到，重新从第一天签到
function M.GetSingInIdContinuity(self)
	--print("---GetSingInId---:",self.data.time)
	--print("----GetSingInId------0")
	if self.data.time==0 then --初次进入游戏，发放第一天奖励
		--print("----GetSingInId------1")
		return 1
	end
	

	local day_s,end_s=self:GetDayStartAndEnd(self.data.time)
	--签到时间已经超过了一天（有中断，发放第一天的奖励)
	if os.time()>end_s then

		local id=self.data.id+1
		if id>MAX_DAY then
			id=1
		end
		return id
	end

	if os.time()>day_s and os.time()<end_s then
		local id=self.data.id+1
		if id>MAX_DAY then
			id=1
		end
		return id
	end

	return self.data.id,true --不能够领取

end;




function M.SingIn(self)
	
	local singId,no=self:GetSingInId()
	if no then
		return false
	else
		self:GetGift(singId)
		return true
	end
end

function M.dtor(self)
	M.super.dtor(self);
end;

local function_list = ServerOHOH.function_list;

M:RegistFunction(function_list.sing_in,M.SingIn)


return M